<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table
      :data="dataList"
      row-key="menuId"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column
        prop="menuName"
        label="菜单名称"
        :show-overflow-tooltip="true"
      ></el-table-column>

      <el-table-column
        prop="orderNum"
        label="排序"
        width="60"
      ></el-table-column>

      <el-table-column label="创建时间" align="center" prop="createTime">
        <!-- <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template> -->
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="210"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            link
            type="primary"
            icon="Plus"
            @click="handleAdd(scope.row)"
            >新增</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="title"
      v-model="open"
      width="680px"
      append-to-body
      @close="close"
    >
      <el-form ref="menuRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="菜单名称" prop="menuName">
              <el-input
                v-model.trim="form.menuName"
                placeholder="请输入菜单名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="orderNum">
              <el-input-number
                v-model="form.orderNum"
                controls-position="right"
                :min="0"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="路由地址" prop="path">
              <el-input v-model.trim="form.path" placeholder="请输入路由地址" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import {
  queryListRequest,
  addMenuRequest,
  editMenuRequest,
  deleteMenuRequest,
} from "@/api/frontMenu/indexMenu.js";
import { ElMessage, ElMessageBox } from "element-plus";

onMounted(() => {
  getList();
});
const open = ref(false);

const serchForm = reactive({});
// 列表数据
const dataList = ref([]);
async function getList() {
  const res = await queryListRequest(serchForm);
  console.log(res.data[0].children);
  dataList.value = res.data[0].children;
}

const rules = {
  menuName: [
    { required: true, message: "菜单名称不能为空", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  path: [
    { required: true, message: "路由地址不能为空", trigger: "blur" },
    { min: 1, max: 99, message: "长度在 1 到 99 个字符", trigger: "blur" },
  ],
};

let form = reactive({
  menuName: "",
  path: "",
  orderNum: 0,
});

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  if (row != null && row.menuId) {
    form.parentId = row.menuId;
  } else {
    form.parentId = 1;
  }
  open.value = true;
  // title.value = "添加菜单";
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  form.menuName = row.menuName;
  form.orderNum = row.orderNum;
  form.parentId = row.parentId;
  form.menuId = row.menuId;
  form.path = row.path;
  open.value = true;
}

function reset() {
  form.menuName = "";
  form.orderNum = 0;
  form.path = "";
  form.parentId = null;
  form.menuId = null;
}

const menuRef = ref(null);
/** 提交按钮 */
function submitForm() {
  menuRef.value.validate(async (valid) => {
    if (valid) {
      if (form.menuId) {
        const res = await editMenuRequest(form);
        if (res.data) {
          ElMessage.success("编辑成功");
          reset();
          open.value = false;
          getList();
        }
        console.log(res);
      } else {
        const res = await addMenuRequest(form);
        if (res.data) {
          ElMessage.success("添加成功");
          reset();
          open.value = false;
          getList();
        }
        console.log(res);
      }
    } else {
      return false;
    }
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
}
//关闭对话框
function close() {
  reset();
}

/** 删除按钮操作 */
function handleDelete(row) {
  ElMessageBox.confirm('是否确认删除名称为"' + row.menuName + '"的数据项?')
    .then(function () {
      return deleteMenuRequest(row.menuId);
    })
    .then(() => {
      getList();
      ElMessage.success("删除成功");
    })
    .catch(() => {});
}
</script>

<style lang="scss" scoped>
.app-container {
}
</style>
